var changePwdCtrl  = new XFApp.client();

XFApp.client.prototype.changePwd = {
		successCode : 10000,
		urlItems : {			
			updatePasswdURL: basePath+"/register/updatePwdById",
			verifyCodeURL : basePath+"/verifyCode/creator?timestamp=",
			verifyCodeCheckURL: basePath+"/verifyCode/check",
			passwordCheckURL: basePath+"/register/passwordCheck",
		},
		
		formValidate : function(){			
			$("#regForm").validate({
				errorPlacement : function(error, element) {
					if (element.is(":radio"))
						error.appendTo(element.parent());
					else if (element.is(":checkbox"))
						error.appendTo(element.parent().parent());
					else if(element.is(":input[name=fourcode]"))
						error.appendTo(element.parent());
					else
						error.insertAfter(element); 
						//error.appendTo(element.parent().next());
				},
		        rules: {
		        	//oldPassword:"required",
		        	password:  {
		        		        required:true,
		        		        minlength:6
		        				},
		        	password2: {
		        		        required:true,
		        		        equalTo: "#password"
		        	           },		        	           
		        	//fourcode: "required"
				  },
				 messages: {	
					 //oldPassword:"请填写旧密码",
			         password: {
			        	        required:"请填写新密码",
			        	        minlength:$.validator.format("新密码长度必须大于{0}个字符"),
			         		   },
			         password2:{
			        	 		required:"请再次输入密码",
			        	 		equalTo:"两次输入的密码不一致"
			         			},			        
			         //fourcode:  "请输入验证码"
			         			
				  }
		    });
			$("#regForm").validate();
		},
		
		event : function (){			
			_this = this;
			
			$("#submitBtn").on("click",function(){	
				
				if(!$("#oldPassword").val()){
					$("#oldPassword-error").html('请填写旧密码');
				}
				
				if(!$("#fourcode").val()){
					$("#fourcode-error").html('请输入验证码');
				}
				
				if($("#regForm").validate().form()){
					if(!$("#fourcode-error").text() && !$("#oldPassword-error").text()){	
						changePwdCtrl.changePwd.submit();		
					}
				}				
			});
			//表单验证
			_this.formValidate();
			
			$("#next").on("click",function(){
				_this.nextImg();
			});
			
			$("#fourcode").on("blur",function(){
				_this.verifyCodeCheck();
			});
			
			$("#oldPassword").on("blur",function(){
				_this.passwordCheck();
			});
			
			//jquery validator 自定义方法
//			$.validator.addMethod("verifyCode", function(value, element) {				 
//				 return this.optional(element) || (_this.verifyCodeCheck(value));
//			});	
			
//			$.validator.addMethod("accountCheck", function(value, element) {				
//				 return this.optional(element) || (_this.checkAccount(value));
//			});	
			
		},	
		
		passwordCheck:function(){
			var params = {
					id : $("#hiddenId").val(),
					password : $("#oldPassword").val()
			}
			$.ajax({
	    		url :_this.urlItems.passwordCheckURL,
	    		data : params,
	    		dataType : "json",
	    		type : 'post',
	    		async:false,
	    		success : function(data) {				
	    			if(data.code !=_this.successCode){ 
	    				$("#oldPassword-error").html(data.msg);
	    			}else{
	    				$("#oldPassword-error").html("");
	    			}
	    		}
	    	});
		},
		
		verifyCodeCheck : function(){		
			var params = {
					verifyCode : $("#fourcode").val(),
					sessionKey:'verifyCode',
			}
			
			$.ajax({
	    		url :_this.urlItems.verifyCodeCheckURL,
	    		data : params,
	    		dataType : "json",
	    		type : 'post',
	    		async:false,
	    		success : function(data) {				
	    			if(data.code !=_this.successCode){ 
	    				$("#fourcode-error").html(data.msg);
	    			}else{
	    				$("#fourcode-error").html("");
	    			}
	    		}
	    	});
		},
		
		nextImg : function(){
			var url = _this.urlItems.verifyCodeURL+(new Date()).valueOf();
			$("#verifyCodeImg").attr("src",url);
		},		
		
		submit : function (){
			
			var entity = {
			        id : $("#hiddenId").val(),					
					password : $("#password").val(),
				}
		    	
		    	$.ajax({
		    		url :_this.urlItems.updatePasswdURL,
		    		data : entity,
		    		dataType : "json",
		    		type : 'post',
		    		success : function(data) {				
		    			if(data.code==_this.successCode){ 
		    				window.webDialogUtil.alert("提示",data.msg);	
		    				$('#regForm')[0].reset();
		    			}else{
		    				window.webDialogUtil.alert("提示",data.msg);
		    			}
		    		}
		    	});	
			
		},
},

$(function(){	
	changePwdCtrl.changePwd.event();
});